package controller;

import dao.UserMapper;
import entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * Created by zjh on 2020/11/17 下午 9:34
 */
//注解
@WebServlet("/loginController")
public class loginController extends HttpServlet {
    @Override
    public void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
        //解决乱码
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");
        //获取前端数据
        String user_name = servletRequest.getParameter("user_name");//用户名
        String password = servletRequest.getParameter("password");//密码
        //用set方法将数据放进user对象里
        User user = new User();
        user.setUsername(user_name);
        user.setPassword(password);
        //实现接口，把数据插入进去
        selectUser(user.getUsername(),user.getPassword());
        //进行用户信息判断
        if(user.getUsername().equals(user_name)&&user.getPassword().equals(password)) {
            servletResponse.getWriter().write("登录成功 ===="+user.getUsername());
            //登录成功3秒后跳转到首页
            servletResponse.setHeader("refresh",
        "3;url=http://localhost:8080/CZBKBookshop_war_exploded/client/index.html");

        }else {
            servletResponse.getWriter().write("登录失败");
            //登录失败3秒后跳转到登录页面
            servletResponse.setHeader("refresh",
                    "3;url=http://localhost:8080/CZBKBookshop_war_exploded/client/login.html");
        }

    }
//    工具类方法

    public static void selectUser(String user_name,String password) throws IOException {
        try {
            //使用MyBatis提供的Resources类加载mybatis的配置文件
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            //构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

            SqlSession session= sessionFactory.openSession();
            //加载sql映射
            UserMapper mapper=session.getMapper(UserMapper.class);
            List<User> list = mapper.selectUser(user_name, password);
            for (entity.User arr: list) {
                System.out.println("user_name："+arr.getUsername()+
                        ";password："+arr.getPassword()+"登录成功。");
            }
            session.commit();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
